<?php



// returns a ResultContext object encoded as JSON, whether operation completes or not.
function handleCreateOperation($request) {
    //array of associative array with 2 entry per array
    //forms a pair of "webpage_id","tags_id"
    $arWebpageHasTags[] = array();
    $webpageId;
    $currentTagId;


//to long to put in place!    $stateResultContext=new ResultContext(); //we use this facility     
    /*    $stateResultContext->isSuccessful(); // we're optimistic: 
      $resultContext=new ResultContext($success, $arReturnParameter, $arFeedbackMsgs) */

    connect();


    /* I've coded the operation below somewhat fast, the hacky way so to speak.
     * may be improved
     */
    //========================1) the webpage
    //Does it exist ?(UNIQUE rule on the field url) ...
    //SELECT id FROM webpage WHERE url='h'
    $arResults = selectFromTable("webpage", "id", "url='" . $request["url"] . "'");
    if (count($arResults) == 0) {
        // ... no it doesn't : no records found. We create it ...
        $resultContext = dbCreateWebPage($request);
        //... then store it
        $webpageId = $resultContext->arReturnParameter["default"]["id"];
    } else {
        // ... yes it does: we store the id of this already existing record then
        $webpageId = $arResults[0]["id"];
    }

    //========================2) we loop through the tags provided. For each tag, we then create the webpage_id,tags_id record
    // We traverse the array of tags provided through the JSON request
    $nbTags = count($request['tags']);
    $currentTagId = -1;
    $webpageHasTagsId = -1;
    $feedback = array();
    for ($i = 0; $i < $nbTags; $i++) {
        $currentTagText = $request['tags'][$i];
        //Does it exist ?(UNIQUE rule on the field url) ...
        //SELECT id FROM webpage WHERE url='h'
        $arResults = selectFromTable("tags", "id", "tag='" . $currentTagText . "'");
        if (count($arResults) == 0) {
            // ... no it doesn't : no records found. We create it ...
            $resultContext = dbCreateTag(array("tag" => $currentTagText));
            //... then store it
//        $newTagId=$resultContext->arReturnParameter["default"];
//            $newTag = $resultContext->arReturnParameter;
            $currentTagId = $resultContext->arReturnParameter["default"]["id"];
        } else {
            // ... yet it does: we store the id of this already existing record then
            $currentTagId = $arResults[0]["id"];
        }


        //Does this pair already exist ?(UNIQUE rule on the field url) ...
        //SELECT id FROM webpage WHERE url='h'
        $arResults = selectFromTable("webpage_has_tags", "id", "webpage_id=$webpageId AND tags_id=$currentTagId");
        if (count($arResults) == 0) {
            // ... no it doesn't : no records found. We create it ...
            //we create the record for this (webpage_id,tags_id) pair 
            $resultContext = dbCreateWebpageHasTags(array(
                "webpage_id" => $webpageId,
                "tags_id" => $currentTagId
                    ));

            $webpageHasTagsId = $resultContext->arReturnParameter["default"]["id"];
            $feedback[$request['tags'][$i]] = "new webpagehastags created! id=$webpageHasTagsId";
        } else {
            // ... yet it does: we store the id of this already existing record then
            $webpageHasTagsId = $arResults[0]["id"];
            $feedback[$request['tags'][$i]] = "webpagehastags already exists with id=$webpageHasTagsId END..";
        }
    }

    $resultContext->arFeedbackMsgs = $feedback;
// ???? $resultContext=dbCreateWebPage($request["url"],$request["title"]);


    echo json_encode($resultContext);
}
?>
